package com.stox.client.workbench.ui.javafx.stage;

import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import com.stox.client.ui.javafx.core.util.CSS;
import com.stox.client.ui.javafx.core.util.UI;
import com.stox.client.workbench.ui.javafx.StoxWorkbench;
import com.stox.client.workbench.ui.javafx.stage.titlebar.TitleBar;

@Component
public class StoxStage extends Stage implements InitializingBean {

	@Autowired
	private ApplicationContext applicationContext;

	private final Pane contentPane = new Pane();
	private final BorderPane root = new BorderPane();
	private TitleBar titleBar = null;
	private Rectangle2D backupWindowBounds = null;
	private final StageResizeRelocateMouseEventHandler stageResizeRelocateMouseEventHandler = new StageResizeRelocateMouseEventHandler();

	private static StoxStage instance;

	public static StoxStage getInstance() {
		return instance;
	}

	public StoxStage() {
		StoxStage.instance = this;
		setTitle("Stox");
		getIcons().add(new ImageView("image/icon-title.png").getImage());

		initStyle(StageStyle.UNDECORATED);
		setSize();
		setOnShowing(new EventHandler<WindowEvent>() {
			@Override
			public void handle(final WindowEvent event) {
				StoxWorkbench.closeSplashStage();
			}
		});
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		setScene(createScene());
		stageResizeRelocateMouseEventHandler.applyStageResizeRelocateMouseEventHandler(this);
		UI.style(contentPane, CSS.stoxStageContentPane);
	}

	public void exit() {
		System.exit(0);
	}

	private Scene createScene() {
		titleBar = new TitleBar(this, applicationContext);
		root.setTop(titleBar);
		root.setCenter(contentPane);
		final Scene scene = new Scene(root, getWidth(), getHeight(), Color.BLACK);
		scene.getStylesheets().add(getClass().getClassLoader().getResource("style/official.css").toExternalForm());
		scene.getStylesheets().add(getClass().getClassLoader().getResource("style/numberspinner.css").toExternalForm());
		return scene;
	}

	private void setSize() {
		final Rectangle2D rectangle = Screen.getPrimary().getVisualBounds();
		setX(rectangle.getMinX());
		setY(rectangle.getMinY());
		setHeight(rectangle.getHeight());
		setWidth(rectangle.getWidth());
	}

	public TitleBar getTitleBar() {
		return titleBar;
	}

	public Rectangle2D getBackupWindowBounds() {
		return backupWindowBounds;
	}

	public void setBackupWindowBounds(final Rectangle2D backupWindowBounds) {
		this.backupWindowBounds = backupWindowBounds;
	}

	public StageResizeRelocateMouseEventHandler getStageResizeRelocateMouseEventHandler() {
		return stageResizeRelocateMouseEventHandler;
	}

	public Pane getContentPane() {
		return contentPane;
	}

}
